package com.yayako.modules.user.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.yayako.modules.user.entity.User;
import com.yayako.modules.user.service.UserService;
import com.yayako.result.Result;
import com.yayako.result.ResultCode;
import com.yayako.result.ResultFactory;
import com.yayako.utils.EasyExcel.ImportListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/** @author cyy */
@Controller
@RequestMapping("/user")
public class UserController {
  @Autowired UserService userService;
  private Result result;

  @RequestMapping("/register")
  @ResponseBody
  public Result save(@RequestBody User user) {
    return userService.register(user);
  }

  @RequestMapping("/data")
  @ResponseBody
  public Result getData() {
    result = ResultFactory.bulidSuccessResult(userService.getData());
    return result;
  }

  @RequestMapping("/edit")
  @ResponseBody
  public Result edit(@RequestBody User user) {
    return userService.edit(user);
  }

  @RequestMapping("/del")
  @ResponseBody
  public Result delete(@RequestParam String id) {
    return userService.delete(id);
  }

  @RequestMapping("/import")
  @ResponseBody
  public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
    ImportListener importListener = new ImportListener();
    EasyExcel.read(file.getInputStream(), User.class, importListener).sheet().doRead();
    Result result = importListener.getResult();
    System.out.println(result);
    return null;
  }

  @RequestMapping("/export")
  public void download(HttpServletResponse response) throws IOException {

    try {
      // 设置头文件
      response.setContentType("application/vnd.ms-excel");
      response.setCharacterEncoding("utf-8");
      EasyExcel.write(response.getOutputStream(), User.class)
          .autoCloseStream(Boolean.FALSE)
          .sheet("1")
          .doWrite(userService.getData());
      result = ResultFactory.bulidSuccessResult(null);
    } catch (Exception e) {
      // 重置response
      response.reset();
      response.setContentType("application/json");
      response.setCharacterEncoding("utf-8");
      Map<String, String> map = new HashMap<String, String>();
      map.put("status", "failure");
      map.put("message", "下载文件失败" + e.getMessage());
      response.getWriter().println(JSON.toJSONString(map));
      result =
          ResultFactory.buildResult(
              ResultCode.INTERNAL_SERVER_ERROR, "下载文件失败", JSON.toJSONString(map));
    }
    //    return result;
  }
}
